--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----
    local Widget = require "widgets/widget"
    local Screen = require "widgets/screen"
    local Image = require "widgets/image" -- 引入image控件
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    -- local SCREEN_OFFSET = 0.15 * RESOLUTION_X
    local function Get_SCREEN_OFFSET()
        -- if ThePlayer and ThePlayer._SCREEN_OFFSET then
        --     return ThePlayer._SCREEN_OFFSET
        -- end
        return 0.4 * RESOLUTION_X
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function CreateInspectHUD(inst)
        local front_root = inst.HUD

        local root = front_root:AddChild(Screen())

        local black = root:AddChild(ImageButton("images/global.xml", "square.tex"))
        black.image:SetVRegPoint(ANCHOR_MIDDLE)
        black.image:SetHRegPoint(ANCHOR_MIDDLE)
        black.image:SetVAnchor(ANCHOR_MIDDLE)
        black.image:SetHAnchor(ANCHOR_MIDDLE)
        black.image:SetScaleMode(SCALEMODE_FILLSCREEN)
        black.image:SetTint(0,0,0,.5)    
        black:SetHelpTextMessage("")
        -----------------------------------------------------------------------------------
        -- 关闭退出
            root.button_close_fn = function()
                TheFrontEnd:PopScreen()
                TheCamera:PopScreenHOffset(root, Get_SCREEN_OFFSET())
                root:Kill()
            end

            inst:DoTaskInTime(10/30,function()
                black:SetOnClick(function()
                    -- root.button_close_fn()
                    root.inst:PushEvent("talent_tree_close")
                end)
            end)
            root.inst:ListenForEvent("talent_tree_close",root.button_close_fn)
        -----------------------------------------------------------------------------------
        --- 页面打开并锁定
            TheCamera:PushScreenHOffset(root, Get_SCREEN_OFFSET())
            inst.HUD:OpenScreenUnderPause(root)
            inst:PushEvent("hutao_event.client_side_inspect_hud_open",root)  --- 触发事件加载 天赋树界面
        -----------------------------------------------------------------------------------
        --- opening flag
            root.inst:ListenForEvent("talent_tree_close",function()
                inst:PushEvent("hutao_event.inspect_hud_warning",false)
            end)
        -----------------------------------------------------------------------------------
        --- 创建任何形式的退出
            local key_handler = TheInput:AddKeyHandler(function(key, down)
                if down then
                    root.inst:PushEvent("talent_tree_close")
                end
            end)
            root.inst:ListenForEvent("onremove",function()
                key_handler:Remove()
            end)
        -----------------------------------------------------------------------------------
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function Hook_Inspect_Button(inst)
        ------------------------------------------------------------------------------------------------
        ------ 创建警示箭头,使用循环任务
            local function arrow_create()
                local inventorybar = ThePlayer == inst and inst.HUD and ThePlayer.HUD.controls.inv
                if inventorybar then
                    local inspect_button = inventorybar.inspectcontrol
                    if inspect_button then               
                        local arrow = inspect_button:AddChild(UIAnim())
                        local arrow_anim = arrow:GetAnimState()
                        arrow_anim:SetBuild("hutao_self_inspect_button_warning")
                        arrow_anim:SetBank("hutao_self_inspect_button_warning")
                        arrow_anim:PlayAnimation("idle",true)
                        arrow:Hide()
                        inst:ListenForEvent("hutao_event.inspect_hud_warning",function(_,flag)
                            if flag then
                                arrow:Show()
                            else
                                arrow:Hide()
                            end
                        end)
                        return true
                    end
                end
                return false
            end
            ---- 创建警示箭头
            if not arrow_create() then
                inst.___inspect_button_task = inst:DoPeriodicTask(1,function()
                    if arrow_create() then
                        inst.___inspect_button_task:Cancel()
                        inst.___inspect_button_task = nil
                    end
                end)
            end
        ------------------------------------------------------------------------------------------------
        --- 入口按钮
            inst.HUD.InspectSelf = function(self)
                if self:IsVisible() and self.owner.components.playercontroller:IsEnabled() then
                    CreateInspectHUD(inst)
                    inst:PushEvent("hutao_event.inspect_hud_warning",false)
                    return true
                end
            end
        ------------------------------------------------------------------------------------------------
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    ---- hook 进 检查按钮。
    inst:DoTaskInTime(0,function()
        if ThePlayer == inst and inst.HUD then
            Hook_Inspect_Button(inst)
        end
    end)

    ---- 警示
    inst:DoTaskInTime(5,function()
        if ThePlayer and ThePlayer == inst then
            if TUNING.HUTAO_FNS:Get_ThePlayer_Cross_Archived_Data("hutao_event.inspect_hud_warning") == true then
                
            else
                TUNING.HUTAO_FNS:Set_ThePlayer_Cross_Archived_Data("hutao_event.inspect_hud_warning",true)
                inst:PushEvent("hutao_event.inspect_hud_warning",true)
            end
            --- 每60秒检查一次突破,并提示
            local function check_level_up_locking()
                if inst.replica.hutao_com_level_sys:IsLevelUpLocking() then
                    inst:PushEvent("hutao_event.inspect_hud_warning",true)
                end
            end
            check_level_up_locking()
            inst:DoPeriodicTask(480,check_level_up_locking)
        end
    end)


    if not TheWorld.ismastersim then
        return
    end

    -- inst:DoTaskInTime(3,function()
    --     local inspect_warning_index = "inspect_hud_first_warning."..tostring(inst.userid)
    --     local flag = inst.components.hutao_data:Get(inspect_warning_index) or TheWorld.components.hutao_data:Get(inspect_warning_index) or false
    --     if not flag then
    --         inst.components.hutao_data:Set(inspect_warning_index,true)
    --         TheWorld.components.hutao_data:Set(inspect_warning_index,true)
    --         inst.components.hutao_com_rpc_event:PushEvent("hutao_event.inspect_hud_warning",true)
    --     else
    --         inst.components.hutao_data:Set(inspect_warning_index,true)
    --         TheWorld.components.hutao_data:Set(inspect_warning_index,true)
    --     end
    -- end)
    local warning_cd_task = nil
    inst:ListenForEvent("hutao_com_level_sys.level_up_locking",function()
        if warning_cd_task then
            return
        end
        inst.components.hutao_com_rpc_event:PushEvent("hutao_event.inspect_hud_warning",true)
        warning_cd_task = inst:DoTaskInTime(600,function()
            warning_cd_task = nil
        end)
    end)


end